define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/course/index' + location.search,
                    add_url: 'course/course/add',
                    edit_url: 'course/course/edit',
                    del_url: 'course/course/del',
                    multi_url: '',
                    import_url: '',
                    table: 'course',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', width:240, title: __('Title'), formatter: Controller.api.formatter.title, operate: 'LIKE'},
                        {field: 'sub_title', width:200, title: __('Sub Title'), formatter: Controller.api.formatter.subtitle, operate: 'LIKE'},
                        {field: 'class_name', title: __('Class_name'), formatter: Controller.api.formatter.classname, searchable:false},
                        {field: 'cover', title: __('Cover'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'tags', width:170, title: __('Tags'), formatter: Controller.api.formatter.tags, searchable:false},
                        {field: 'sort', title: __('Recommend'),searchable:false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {1: __('Publish'), 0: __('Unpublish')}},
                        {
                            field: 'chapter',
                            width: "120px",
                            title: __('课程章节'),
                            operate:false,
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'chapter',
                                    text: __('章节查看'),
                                    title: __('章节列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-clipboard',
                                    url: 'course/chapter/index/course_id/{id}',
                                    
                                },  
                                /*{   
                                    name: 'detail',
                                    text: '添加',
                                    title: '章节添加',
                                    icon: 'fa fa-list', 
                                    classname: 'btn btn-xs btn-primary btn-success btn-dialog',
                                    url: 'course/chapter/add'
                                }*/
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        
                        {
                            field: 'member',
                            width: "120px",
                            title: __('课程学员'),
                            operate:false,
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'member',
                                    text: __('学员列表'),
                                    title: __('学员列表'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-user-circle-o',
                                    url: 'course/member/index/course_id/{id}',
                                    
                                },
                                /*{  
                                    name: 'member', 
                                    text: '添加',
                                    title: '添加成员', 
                                    icon: 'fa fa-list',
                                    classname: 'btn btn-xs btn-primary btn-success btn-dialog', 
                                    url: 'course/member/add'
                                },*/
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table,events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        member: function () {
            Controller.api.bindevent();
        },
       
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: { //渲染的方法
                title: function(value, row, index) {
                   var title_html = "<div style='white-space: initial;'>"+row.title+"</div>";
                    return title_html;
                },
                subtitle: function(value, row, index) {
                    var subtitle_html = "<div style='white-space: initial;color: #666666;'>"+row.sub_title+"</div>";
                     return subtitle_html;
                },
                classname: function(value, row, index) {
                    var class_html = "<div style='font-weight: bold;'>"+row.class_name+"</div>";
                     return class_html;
                },
                tags: function(value, row, index) {
                    var tags_html = "<div style='display: flex;flex-wrap: wrap;'>";
                    $.each(row.tags, function (i, j) {
                        if(j != undefined){
                            tags_html += '<span class="label label-success" style="margin: 2px 0;">'+j+'</span>&nbsp;';
                        }
                    });
                    tags_html += "</div>";
                    return tags_html;
                },
            }
        }
    };
    return Controller;
});